import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import '../../../hj_utils/utils.dart';
import '../../../tools_base/widget/net_image_widget.dart';

class AiNewImageView extends StatefulWidget {
  final String imageUrl;

  const AiNewImageView({super.key, required this.imageUrl});

  @override
  State<StatefulWidget> createState() {
    return _AiNewImageViewState();
  }
}

class _AiNewImageViewState extends State<AiNewImageView> {
  final GlobalKey _globalKey = GlobalKey();
  @override
  void initState() {
    super.initState();
  }

  void _saveEvent() async {
    Future.delayed(const Duration(milliseconds: 20), () async {
      RenderRepaintBoundary? boundary = _globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
      var image = await boundary.toImage(pixelRatio: 3.0);
      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      await savePngDataToAblumn(byteData?.buffer.asUint8List());
      // if (suc) {
      //showToast( Lang.SAVE_PHOTO_ALBUM);
      // }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          RepaintBoundary(
            key: _globalKey,
            child: Container(
              //width: 1.sw,
              color: Colors.transparent,
              alignment: Alignment.center,
              child: NetworkImageLoader(
                imageUrl: widget.imageUrl,
                fit: BoxFit.fitWidth,
                width: double.infinity,
              ),
            ),
          ),
          Positioned(
            left: 16,
            child: InkWell(
              onTap: () => {Navigator.pop(context)},
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 16.h),
                child: SvgPicture.asset(
                  'assets/svg/back_icon.svg',
                  width: 20.w,
                  height: 20.w,
                ),
              ),
            ),
          ),
          Positioned(
            right: 16,
            top: 10,
            child: InkWell(
              onTap: _saveEvent,
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  color: const Color(0xFFE57310),
                ),
                child: const Text(
                  "保存到相册",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
